let VS_move = 0.0
let VS_levelX = 0.0
let VS_perpendicularityY = 0.0
let VS_type = 0.0
let VS_min = -170
let VS_max = 170
let VS_status = 2
let VS_plan = null
let VS_show = true
let distance = 0.0

/**
 * 改变滑动条
 * @param type
 */
const VS_change = (type = 1) => {
  VS_status = type
  VS_clearCutting()
  if(type === 1) {
    VS_loadTileset(new Cesium.Cartesian3(0.0, -1.0, 0.0))
  } else if(type === 2) {
    VS_loadTileset(new Cesium.Cartesian3(-1.0, 0.0, 0.0))
  } else if(type === 3) {
    VS_loadTileset(new Cesium.Cartesian3(0.0, 0.0, -1.0))
  }
}

/**
 * 开启立切
 * @param Cartesian3
 * @returns {Promise<unknown>}
 */
const VS_loadTileset = (Cartesian3 = new Cesium.Cartesian3(-1.0, 0.0, 0.0)) => {
  const clippingPlanes = new Cesium.ClippingPlaneCollection({
    planes: [
      // ClippingPlane对象数组集合
      new Cesium.ClippingPlane(Cartesian3, 0.0) // 法线方向
    ],
    // edgeColor: Cesium.Color.RED,
    edgeWidth: 1.0, // 模型被裁切部分的截面线宽
    // unionClippingRegions: true //true 才能多个切割
  })
  // 定义一个ClippingPlaneCollection类，用来存储裁剪面
    for (let globalTilesetArrKey of globalTilesetArr) {
      globalTilesetArrKey.clippingPlanes = clippingPlanes
      // const boundingSphere = VS_tileset.boundingSphere
      // const radius = boundingSphere.radius
      if(
          !Cesium.Matrix4.equals(
              globalTilesetArrKey.root.transform,
              Cesium.Matrix4.IDENTITY
          )
      ) {
        const transformCenter = Cesium.Matrix4.getTranslation(
            globalTilesetArrKey.root.transform,
            new Cesium.Cartesian3()
        )
        const transformCartographic = Cesium.Cartographic.fromCartesian(
            transformCenter
        )
        const boundingSphereCartographic = Cesium.Cartographic.fromCartesian(
            globalTilesetArrKey.boundingSphere.center
        )
        const height =
            boundingSphereCartographic.height -
            transformCartographic.height
        clippingPlanes.modelMatrix = Cesium.Matrix4.fromTranslation(
            new Cesium.Cartesian3(0.0, 0.0, height)
        )
      }
    }
    for(let i = 0; i < clippingPlanes.length; ++i) {
      const plane = clippingPlanes.get(i)
      const planeEntity = viewer.entities.add({
        position: globalTilesetArr[0].boundingSphere.center,
        id: 'ClipPlane01',
        plane: {
          dimensions: new Cesium.Cartesian2(globalTilesetArr[0].boundingSphere.radius * 2, globalTilesetArr[0].boundingSphere.radius * 2),
          material: Cesium.Color.WHITE.withAlpha(0.2),
          plane: new Cesium.CallbackProperty(VS_createPlaneUpdateFunction(plane), false)
          // outline : true,
          // outlineColor : Cesium.Color.WHITE
        }
      })
      // planeEntity.plane.show = false
      console.log(planeEntity)
      VS_plan = planeEntity
    }
}

/**
 * 退出立剖
 */
const VS_clearCutting = () => {
  for (let vsTilesetKey of globalTilesetArr) {
    vsTilesetKey.clippingPlanes = new Cesium.ClippingPlaneCollection()
  }
  console.log('2')
  viewer.entities.removeById('ClipPlane01')
  VS_move = 0.0
  VS_levelX = 0.0
  VS_perpendicularityY = 0.0
  VS_show = true
  VS_plan = null
}

// 计算移动位置
const VS_createPlaneUpdateFunction = (plane) => {
  return function() {
    // console.log(VS_move)
    let height = VS_move + VS_levelX / 1000000 + VS_perpendicularityY / 1000000
    // if(VS_levelX !== 0.0 || VS_perpendicularityY !== 0.0){
    //     height = (height - distance) / 10000 + distance
    // }
    if(VS_status === 1) {
      if(VS_type === 1) {
        plane.distance = height
      } else if(VS_type === 2) {
        plane.distance = height
        plane.normal._cartesian3 = new Cesium.Cartesian3(VS_levelX / 10, plane.normal.y, plane.normal.z)
      } else if(VS_type === 3) {
        plane.distance = height
        plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x, plane.normal.y, VS_perpendicularityY / 10)
      }
    } else if(VS_status === 2) {
      if(VS_type === 1) {
        plane.distance = height
      } else if(VS_type === 2) {
        plane.distance = height
        plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x, plane.normal.y, VS_levelX / 10)
      } else if(VS_type === 3) {
        plane.distance = height
        plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x, VS_perpendicularityY / 10, plane.normal.z)
      }
    } else if(VS_status === 3) {
      if(VS_type === 1) {
        plane.distance = height
      } else if(VS_type === 2) {
        plane.distance = height
        plane.normal._cartesian3 = new Cesium.Cartesian3(VS_levelX / 10, plane.normal.y, plane.normal.z)
      } else if(VS_type === 3) {
        plane.distance = height
        plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x, VS_perpendicularityY / 10, plane.normal.z)
      }
    }
    distance = plane.distance
    return plane
  }
}
/**
 * 显影剖面
 */
const developPlan = () => {
  if (VS_show){
    VS_plan.plane.material = Cesium.Color.WHITE.withAlpha(0)
    VS_show = false
  }else {
    VS_show = true
    VS_plan.plane.material = Cesium.Color.WHITE.withAlpha(0.2)
  }
}
